<template>
    <d2-container type="card">
        <header>
            <el-form :inline="true" :model="selectFrom" class="demo-form-inline" size="mini">
                <el-form-item label="商品名称">
                    <el-input v-model="selectFrom.name" placeholder="请输入商品名称" clearable></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="getIntegralCommodityData()">查询</el-button>
                    <el-button type="primary" @click="addIntegralCommodityData()">新增</el-button>
                </el-form-item>
            </el-form>
        </header>
        <footer>
            <el-table :data="tableData" border style="width: 100%" size="mini" :height="height">
                <el-table-column prop="imageUrlList" label="商品图片" align="center">
                    <template slot-scope="scope">
                    <div class="demo-image__preview">
                        <el-image
                        ref="preview"
                        style="width: 80px; height: 80px"
                        :src="scope.row.imageUrlList[0]"
                        :preview-src-list="scope.row.imageUrlList"
                        >
                        </el-image>
                    </div>
                    </template>
                </el-table-column>
                <el-table-column prop="posterUrlList" label="商品海报" align="center">
                    <template slot-scope="scope">
                    <div class="demo-image__preview">
                        <el-image
                        ref="preview"
                        style="width: 80px; height: 80px"
                        :src="scope.row.posterUrlList[0]"
                        :preview-src-list="scope.row.posterUrlList"
                        >
                        </el-image>
                    </div>
                    </template>
                </el-table-column>
                <el-table-column prop="id" label="积分商品ID" align="center"></el-table-column>
                <el-table-column prop="name" label="名称" align="center"></el-table-column>
                <el-table-column prop="integralCommodityType.name" label="商品分类" align="center"></el-table-column>
                <el-table-column prop="state" label="状态" align="center" :show-overflow-tooltip="true">
                    <template slot-scope="scope">
                        <div v-if="scope.row.state == 0">上架</div>
                        <div v-if="scope.row.state == 1">下架</div>
                        <div v-if="scope.row.state == 2">售完</div>
                    </template>
                </el-table-column>
                <el-table-column prop="salesVolume" label="销量" align="center"></el-table-column>
                <el-table-column prop="integral" label="积分" align="center"></el-table-column>
                <el-table-column prop="createTime" label="创建时间" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="updateIntegralCommodityData(scope.row)">修改</el-button>
                        <el-popconfirm title="确定删除该商品么？" @confirm="deleteIntegralCommodityData(scope.row)">
                            <el-button slot="reference" size="mini" style="margin:5px" type="danger">删除</el-button>
                        </el-popconfirm>
                    </template>
                </el-table-column>
            </el-table>
            <div class="block" style="text-align:center">
                <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="page"
                    :page-sizes="[5, 10, 15, 20]" :page-size="limit" layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
                </el-pagination>
            </div>
        </footer>
        <el-dialog :title="title" :visible.sync="integralCommoditySign" width="30%">
            <el-form :model="integralCommodityFrom" :rules="rules" ref="integralCommodityFrom" label-width="100px" class="demo-ruleForm">
                <el-form-item label="商品图片" prop="image">
                    <el-input v-model="integralCommodityFrom.image" placeholder="请输入图片"></el-input>
                </el-form-item>
                <el-form-item label="商品海报" prop="poster">
                    <el-input v-model="integralCommodityFrom.poster" placeholder="请输入海报"></el-input>
                </el-form-item>
                <el-form-item label="名称" prop="name">
                    <el-input v-model="integralCommodityFrom.name" placeholder="请输入名称"></el-input>
                </el-form-item>
                <el-form-item label="商品分类" prop="commodityTypeId">
                    <el-input v-model="integralCommodityFrom.commodityTypeId" placeholder="请输入商品分类"></el-input>
                </el-form-item>
                <el-form-item label="状态" prop="state">
                    <el-input v-model="integralCommodityFrom.state" placeholder="请输入状态"></el-input>
                </el-form-item>
                <el-form-item label="积分" prop="integral">
                    <el-input v-model="integralCommodityFrom.integral" placeholder="请输入积分"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="submit()">取 消</el-button>
                <el-button type="primary" @click="submit()">确 定</el-button>
            </span>
        </el-dialog>
    </d2-container>
</template>
  
<script>
import { getIntegralCommodity, addIntegralCommodity, updateIntegralCommodity, deleteIntegralCommodity } from "@/api/modules/integral_commodity.js"
export default {
    name: 'IntegralCommodity',
    data() {
        return {
            selectFrom: {},//搜索栏
            tableData: [],//表格数据
            page: 1,
            limit: 10,
            total: 0,
            height: window.innerHeight - 250, //表格高度
            title: "添加积分商品",
            integralCommoditySign: false,
            isUpdate: false,//是否修改
            integralCommodityFrom: {},//用户规则表单
            rules: {
                proportion: [
                    { required: true, message: '请输入积分商品', trigger: 'blur' },
                ]
            },//规则
        }
    },
    mounted() {
        this.getIntegralCommodityData();
        //-------------------------表格响应----------------//
        this.createHeight();
        //当页面发生变化时，表格高度固定
        window.onresize = () => {
            return (() => {
                this.$nextTick(() => {
                    this.createHeight();
                });
            })();
        };
    },
    methods: {
        //删除积分商品
        deleteIntegralCommodityData(row) {
            deleteIntegralCommodity(row).then(res => {
                this.getIntegralCommodityData();
            })
        },
        //添加积分商品
        addIntegralCommodityData() {
            this.title = "添加积分商品";
            this.integralCommoditySign = true;
            this.isUpdate = false;
        },
        //修改积分商品
        updateIntegralCommodityData(row) {
            this.title = "修改积分商品";
            this.integralCommoditySign = true;
            this.isUpdate = true;
            this.integralCommodityFrom = JSON.parse(JSON.stringify(row));
        },
        //关闭清空积分商品表单
        closeIntegralCommodityData() {
            this.integralCommoditySign = false;
            this.cleanIntegralCommodityData();
        },
        //清空积分商品
        cleanIntegralCommodityData() {
            this.integralCommodityFrom = {};
            if (this.$refs['integralCommodityFrom']) {
                this.$refs['integralCommodityFrom'].resetFields();
            }
        },
        submit() {
            this.$refs['integralCommodityFrom'].validate((valid) => {
                if (valid) {
                    if (this.isUpdate) {
                        updateIntegralCommodity(this.integralCommodityFrom).then(res => {
                            this.closeIntegralCommodityData();
                            this.getIntegralCommodityData();
                        })
                    } else {
                        addIntegralCommodity(this.integralCommodityFrom).then(res => {
                            this.closeIntegralCommodityData();
                            this.getIntegralCommodityData();
                        })
                    }
                } else {
                    return false;
                }
            });
        },
        //获取积分商品
        getIntegralCommodityData() {
            this.selectFrom.page = this.page;
            this.selectFrom.limit = this.limit;
            getIntegralCommodity({}).then(res => {
                this.tableData = res.dataList;
                this.total = res.total;
            })
        },
        handleSizeChange(val) {
            this.limit = val;
            this.getIntegralCommodityData();
        },
        handleCurrentChange(val) {
            this.page = val;
            this.getIntegralCommodityData();
        },
        //---------------自动变更表格高度------------------//
        createHeight() {
            this.height = window.innerHeight - 250;
        },
    }
}
</script>
  